package com.example.dragcontentviewdemo.helper.internal.util;

import android.view.MotionEvent;
import android.view.View;


/**
 * 工具类
 */
public class CaculatorUtil {
    static public float calculateTranslationXFraction(View view){
        try {
            float fraction = view.getMeasuredWidth() > 0 ? view.getTranslationX() / view.getMeasuredWidth() : 0.f;
            fraction = Math.max(0.f, Math.min(1.f, fraction)); //防止fraction超出范围
            return fraction;
        }
        catch (ArithmeticException exception){ //可能会出现divideByZero的问题。
            return 0.f;
        }
    }

    static public float distance(MotionEvent e1, MotionEvent e2){
        final float deltaX = e1.getRawX() - e2.getRawX();
        final float deltaY = e2.getRawY() - e2.getRawY();
        return (float) Math.sqrt( deltaX * deltaX + deltaY * deltaY);
    }

    static public float calculateSlope(final float x1,final float x2, final float y1,final float y2){
        return (y1 - y2) / (x1 - x2) ;
    }
}
